/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.CTon_Kho;
import DTO.CFields;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class CTonKhoDAO extends IData<CTon_Kho>{
    public CTonKhoDAO(){}

    @Override
    protected Object OnSubmit(CTon_Kho item, int t, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected CTon_Kho getItem(ResultSet rs) {
        CTon_Kho item = new CTon_Kho();
        try {
            item.setId(rs.getLong(CFields.id));
            item.setKhoId(rs.getLong(CFields.kho_id));
            item.setSanPhamId(rs.getLong(CFields.san_pham_id));
            item.setSoLuong(rs.getInt(CFields.so_luong));
            item.setSoLuongNhap(rs.getInt(CFields.so_luong_nhap));
            item.setSoLuongXuat(rs.getInt(CFields.so_luong_xuat));            
        } catch (SQLException ex) {
            Logger.getLogger(CTonKhoDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return item;
    }
    @Override
    public List<CTon_Kho> getAll(SubConfig config) {
        String mysql ="select * from " + CFields.tb_ton_kho + " " + this.sWhere ;
        return this.getAll(mysql, config);
    }

    @Override
    public Object insertOnSubmit(CTon_Kho item, SubConfig config) {
        Object result = 0;
        String mysql  = "insert into " + CFields.tb_ton_kho + "("
                + CFields.san_pham_id + "," + CFields.kho_id + ","
                + CFields.ton_toi_da + "," + CFields.ton_toi_thieu + ","
                + CFields.so_luong_nhap + "," + CFields.so_luong_xuat +","
                + CFields.ten_san_pham
                +") values(?,?,?,?,?,?,?)";
        try {
            PreparedStatement cs = config.connec.prepareStatement(mysql,PreparedStatement.RETURN_GENERATED_KEYS);
            cs.setLong(1, item.getSanPhamId());
            cs.setLong(2, item.getKhoId());
            cs.setInt(3, item.getTonToiDa());
            cs.setInt(4, item.getTonToiThieu());
            cs.setInt(5, item.getSoLuongNhap());
            cs.setInt(6, item.getSoLuongXuat());
            cs.setString(7, item.getTenSanPham());
            cs.executeUpdate();
            ResultSet keyResultSet = cs.getGeneratedKeys();
            if (keyResultSet.next()) {
                result = (long) keyResultSet.getInt(1);      
            }
        } catch (SQLException ex) {
            Logger.getLogger(CTonKhoDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }
    
    @Override
    public Object updateOnSubmint(CTon_Kho item, SubConfig config) {
          Object result = 0;
        String mysql  = "update " + CFields.tb_ton_kho + " set "                                
                + CFields.so_luong_nhap + "=?," + CFields.so_luong_xuat + "=? where "                
                + CFields.san_pham_id + " = ? and " + CFields.kho_id + "=?"                
                +") values(?,?,?,?)";
        try {
            PreparedStatement cs = config.connec.prepareStatement(mysql);
            cs.setInt(1, item.getSoLuongNhap());
            cs.setInt(2, item.getSoLuongXuat());
            cs.setLong(3, item.getSanPhamId());
            cs.setLong(4, item.getKhoId());            
            cs.executeUpdate();
           result = item.getId();
        } catch (SQLException ex) {
            Logger.getLogger(CTonKhoDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public Object deleteOnSubmint(CTon_Kho item, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public CTon_Kho Find(CTon_Kho item, List<CTon_Kho> list) {        
        for(CTon_Kho m : list){
            if(m.getKhoId() == item.getKhoId() && m.getSanPhamId() == item.getSanPhamId())
                return item;
        }
        return null;
    }

    @Override
    public CTon_Kho add(CTon_Kho item) {
          try {            
            ps.setInt(1, item.getSoLuongNhap());
            ps.setInt(2, item.getSoLuongXuat());
            ps.setLong(3, item.getSanPhamId());
            ps.setLong(4, item.getKhoId());      
            ps.addBatch();
        } catch (SQLException ex) {
            Logger.getLogger(CTonKhoDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return item;
    }

    @Override
    public void saveBatch() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void addrows(List<CTon_Kho> list, SubConfig config) {
        try {
            String mysql  = "insert into " + CFields.tb_ton_kho + "("
                    + CFields.san_pham_id + "," + CFields.kho_id + ","
                    + CFields.ton_toi_da + "," + CFields.ton_toi_thieu + ","
                    + CFields.so_luong_nhap + "," + CFields.so_luong_xuat +","
                    + CFields.ten_san_pham
                    +") values(?,?,?,?,?,?,?)";
            ps = config.connec.prepareStatement(mysql);
             for(CTon_Kho item : list){
                this.add(item);
            }
            ps.executeBatch();
            ps.close();
        } catch (SQLException ex) {
            Logger.getLogger(CTonKhoDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

}
